<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="接收状态" prop="isAccept">
        <el-select v-model="queryParams.isAccept" placeholder="请选择接收状态">
          <el-option
            v-for="dict in dict.type.accept_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="维修状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择维修状态">
          <el-option
            v-for="dict in dict.type.repair_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:sysRepairInfo:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:sysRepairInfo:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sysRepairInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="维修厂id" align="center" prop="maintenanceId"/>
      <el-table-column label="接收状态" align="center" prop="isAccept">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.accept_state" :value="scope.row.isAccept"/>
        </template>
      </el-table-column>
      <el-table-column label="维修状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.repair_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleCheckCar(scope.row)"
          >查看车况数据
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleResultCar(scope.row)"
          >查看车况分析
          </el-button>
          <br>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="carInfo(scope.row)"
          >车辆信息
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAccept(scope.row)"
            v-if="scope.row.isAccept === 0"
          >接收
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDeny(scope.row)"
            v-if="scope.row.isAccept === 0"
          >拒绝接收
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleComplete(scope.row)"
            v-if="scope.row.state === 1"
          >完成
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改维修信息表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!--优化-->
        <el-form-item label="维修厂id" prop="maintenanceId">
          <el-input v-model="form.maintenanceId" placeholder="请输入维修厂id"/>
        </el-form-item>
        <el-form-item label="接收状态" prop="isAccept">
          <el-select v-model="form.isAccept" placeholder="请选择接收状态">
            <el-option
              v-for="dict in dict.type.accept_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="维修状态" prop="state">
          <el-select v-model="form.state" placeholder="请选择维修状态">
            <el-option
              v-for="dict in dict.type.repair_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改车况信息对话框 -->
    <el-dialog :title="title" :visible.sync="stateOpen" width="500px" append-to-body>
      <el-form ref="stateForm" :model="stateForm" :rules="rules" label-width="160px">
        <!--优化为车牌号-->
        <el-form-item label="车辆id" prop="carId">
          <el-input v-model="stateForm.carId" placeholder="请输入车辆id"/>
        </el-form-item>
        <el-form-item label="故障码" prop="errorCode">
          <el-input v-model="stateForm.errorCode" placeholder="请输入故障码: 逗号分割"/>
        </el-form-item>
        <el-form-item label="车速" prop="speed">
          <el-input v-model="stateForm.speed" placeholder="请输入车速"/>
        </el-form-item>
        <el-form-item label="节气门开度" prop="jqmKd">
          <el-input v-model="stateForm.jqmKd" placeholder="请输入节气门开度"/>
        </el-form-item>
        <el-form-item label="电源电压" prop="dyDy">
          <el-input v-model="stateForm.dyDy" placeholder="请输入电源电压"/>
        </el-form-item>
        <el-form-item label="发动机转速" prop="fdjZs">
          <el-input v-model="stateForm.fdjZs" placeholder="请输入发动机转速"/>
        </el-form-item>
        <el-form-item label="发动机负载" prop="fdjFh">
          <el-input v-model="stateForm.fdjFh" placeholder="请输入发动机负载"/>
        </el-form-item>
        <el-form-item label="发动机运行时间" prop="fdjRunTime">
          <el-date-picker clearable
                          v-model="form.stateForm"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择发动机运行时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发动机机油油位" prop="fdjJyyw">
          <el-select v-model="stateForm.fdjJyyw" placeholder="机油油位状态">
            <el-option
              v-for="dict in dict.type.flag_open"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="冷却液温度" prop="lqyWd">
          <el-input v-model="stateForm.lqyWd" placeholder="请输入冷却液温度"/>
        </el-form-item>
        <el-form-item label="废气再循环阀设定位置" prop="fqzxhfSdwz">
          <el-input v-model="stateForm.fqzxhfSdwz" placeholder="请输入废气再循环阀设定位置"/>
        </el-form-item>
        <el-form-item label="废气再循环阀实际位置" prop="fqzxhfSjwz">
          <el-input v-model="stateForm.fqzxhfSjwz" placeholder="请输入废气再循环阀实际位置"/>
        </el-form-item>
        <el-form-item label="废气再循环阀反馈电压" prop="fqzxhfFkdy">
          <el-input v-model="stateForm.fqzxhfFkdy" placeholder="请输入废气再循环阀反馈电压"/>
        </el-form-item>
        <el-form-item label="氧传感器超过中值次数" prop="ycgqcgzzCs">
          <el-input v-model="stateForm.ycgqcgzzCs" placeholder="请输入氧传感器超过中值次数"/>
        </el-form-item>
        <el-form-item label="喷油脉宽" prop="pyMk">
          <el-input v-model="stateForm.pyMk" placeholder="请输入喷油脉宽"/>
        </el-form-item>
        <el-form-item label="空气流量" prop="kqLl">
          <el-input v-model="stateForm.kqLl" placeholder="请输入空气流量"/>
        </el-form-item>
        <el-form-item label="进气歧管绝对压力" prop="jqqgJdyl">
          <el-input v-model="stateForm.jqqgJdyl" placeholder="请输入进气歧管绝对压力"/>
        </el-form-item>
        <el-form-item label="进气温度" prop="jqWd">
          <el-input v-model="stateForm.jqWd" placeholder="请输入进气温度"/>
        </el-form-item>
        <el-form-item label="总计缺火故障" prop="zjqhgz">
          <el-input v-model="stateForm.zjqhgz" placeholder="请输入总计缺火故障"/>
        </el-form-item>
        <el-form-item label="自动变速器油温度" prop="zdbsqyWd">
          <el-input v-model="stateForm.zdbsqyWd" placeholder="请输入自动变速器油温度"/>
        </el-form-item>
        <el-form-item label="变速器油温度" prop="bsqyWd">
          <el-input v-model="stateForm.bsqyWd" placeholder="请输入变速器油温度"/>
        </el-form-item>
        <el-form-item label="变矩器锁止离合器" prop="bjqszlhq">
          <el-select v-model="stateForm.bjqszlhq" placeholder="请选择变矩器锁止离合器状态 开/关">
            <el-option
              v-for="dict in dict.type.flag_open"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="变速器" prop="bsq">
          <el-input v-model="stateForm.bsq" placeholder="请输入变速器"/>
        </el-form-item>
        <el-form-item label="左前车轮转速" prop="zqClzs">
          <el-input v-model="stateForm.zqClzs" placeholder="请输入左前车轮转速"/>
        </el-form-item>
        <el-form-item label="右前车轮转速" prop="yqClzs">
          <el-input v-model="stateForm.yqClzs" placeholder="请输入右前车轮转速"/>
        </el-form-item>
        <el-form-item label="左后车轮转速" prop="zhClzs">
          <el-input v-model="stateForm.zhClzs" placeholder="请输入左后车轮转速"/>
        </el-form-item>
        <el-form-item label="右后车轮转速" prop="yhClzs">
          <el-input v-model="stateForm.yhClzs" placeholder="请输入右后车轮转速"/>
        </el-form-item>
        <el-form-item label="ABS电压" prop="absDy">
          <el-input v-model="stateForm.absDy" placeholder="请输入ABS电压"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改检测结果对话框 -->
    <el-dialog :title="title" :visible.sync="resultOpen" width="500px" append-to-body>
      <el-form ref="resultForm" :model="resultForm" :rules="rules" label-width="100px">
        <el-form-item label="车况信息id" prop="stateInfoId">
          <el-input v-model="resultForm.stateInfoId"/>
        </el-form-item>
        <el-form-item label="故障数" prop="errorNum">
          <el-input v-model="resultForm.errorNum" />
        </el-form-item>
        <el-form-item label="故障信息" prop="errorInfo">
          <el-input v-model="resultForm.errorInfo" type="textarea" :autosize="{ minRows: 2, maxRows: 10}"/>
        </el-form-item>
        <el-form-item label="创建时间" prop="createAt">
          <el-date-picker clearable
                          v-model="resultForm.createAt"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改车辆信息表对话框 -->
    <el-dialog :title="title" :visible.sync="carOpen" width="500px" append-to-body>
      <el-form ref="carForm" :model="carForm" :rules="rules" label-width="100px">
        <el-form-item label="车牌号" prop="carNum">
          <el-input v-model="carForm.carNum" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="车辆颜色" prop="carColor">
          <el-input v-model="carForm.carColor" placeholder="请输入车辆颜色" />
        </el-form-item>
        <el-form-item label="车辆类型" prop="carType">
          <el-input v-model="carForm.carType" placeholder="请输入车辆类型" />
        </el-form-item>
        <el-form-item label="制造厂商" prop="createBy">
          <el-input v-model="carForm.createBy" placeholder="请输入制造厂商" />
        </el-form-item>
        <el-form-item label="车辆型号" prop="carModel">
          <el-input v-model="carForm.carModel" placeholder="请输入车辆型号" />
        </el-form-item>
        <el-form-item label="底盘型号" prop="chassisModel">
          <el-input v-model="carForm.chassisModel" placeholder="请输入底盘型号" />
        </el-form-item>
        <el-form-item label="发动机型号" prop="engineModel">
          <el-input v-model="carForm.engineModel" placeholder="请输入发动机型号" />
        </el-form-item>
        <el-form-item label="电池型号" prop="chargeModel">
          <el-input v-model="carForm.chargeModel" placeholder="请输入电池型号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listSysRepairInfo,
    getSysRepairInfo,
    delSysRepairInfo,
    addSysRepairInfo,
    updateSysRepairInfo
  } from "@/api/system/sysRepairInfo";
  import {getCarStateInfo} from "@/api/system/carStateInfo";
  import {getResults} from "@/api/system/results";
  import {getCarByStateId} from "../../../api/system/cars";

  export default {
    name: "SysRepairInfo",
    dicts: ['accept_state', 'repair_state', 'flag_open'],
    data() {
      return {
        stateForm: {},
        stateOpen: false,
        resultForm: {},
        resultOpen: false,
        carForm: {},
        carOpen: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 维修信息表表格数据
        sysRepairInfoList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          stateInfoId: null,
          resId: null,
          maintenanceId: null,
          isAccept: null,
          state: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
    },
    methods: {
      handleCheckCar(row) {
        getCarStateInfo(row.stateInfoId).then(response => {
          this.stateForm = response.data;
          this.stateOpen = true;
          this.title = "修改车况信息";
        });
      },
      handleResultCar(row) {
        getResults(row.resId).then(response => {
          this.resultForm = response.data;
          this.resultOpen = true;
          this.title = "车况检测结果";
        });
      },
      carInfo(row) {
        getCarByStateId(row.stateInfoId).then(res => {
          this.carForm = res.data;
          this.carOpen = true;
          this.title = "车辆信息";
        })
      },
      /** 查询维修信息表列表 */
      getList() {
        this.loading = true;
        listSysRepairInfo(this.queryParams).then(response => {
          this.sysRepairInfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.stateOpen = false;
        this.resultOpen = false;
        this.carOpen = false;
        this.reset();
        this.stateReset();
        this.resultReset();
        this.carReset();
      },
      resultReset() {
        this.resultForm = {
          resId: null,
          stateInfoId: null,
          errorNum: null,
          errorInfo: null,
          createAt: null
        };
        this.resetForm("resultForm");
      },
      stateReset() {
        this.stateForm = {
          infoId: null,
          carId: null,
          errorCode: null,
          speed: null,
          jqmKd: null,
          dyDy: null,
          fdjZs: null,
          fdjFh: null,
          fdjRunTime: null,
          fdjJyyw: null,
          lqyWd: null,
          fqzxhfSdwz: null,
          fqzxhfSjwz: null,
          fqzxhfFkdy: null,
          ycgqcgzzCs: null,
          pyMk: null,
          kqLl: null,
          jqqgJdyl: null,
          jqWd: null,
          zjqhgz: null,
          zdbsqyWd: null,
          bsqyWd: null,
          bjqszlhq: null,
          bsq: null,
          zqClzs: null,
          yqClzs: null,
          zhClzs: null,
          yhClzs: null,
          absDy: null,
          createAt: null
        };
        this.resetForm("stateForm");
      },
      carReset() {
        this.carForm = {
          carId: null,
          carNum: null,
          carColor: null,
          carType: null,
          createBy: null,
          carModel: null,
          chassisModel: null,
          engineModel: null,
          chargeModel: null
        };
        this.resetForm("carForm");
      },
      // 表单重置
      reset() {
        this.form = {
          repairId: null,
          stateInfoId: null,
          resId: null,
          maintenanceId: null,
          isAccept: null,
          state: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.repairId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加维修信息表";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const repairId = row.repairId || this.ids
        getSysRepairInfo(repairId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改维修信息表";
        });
      },
      handleAccept(row) {
        row.isAccept = 1;
        updateSysRepairInfo(row).then(response => {
          this.$modal.msgSuccess("已接收");
          this.open = false;
          this.getList();
        });
      },
      handleDeny(row) {
        row.isAccept = 2;
        updateSysRepairInfo(row).then(response => {
          this.$modal.msgSuccess("已决绝接收");
          this.open = false;
          this.getList();
        });
      },
      handleComplete(row) {
        row.state = 2;
        updateSysRepairInfo(row).then(response => {
          this.$modal.msgSuccess("已完成");
          this.open = false;
          this.getList();
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.repairId != null) {
              updateSysRepairInfo(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addSysRepairInfo(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const repairIds = row.repairId || this.ids;
        this.$modal.confirm('是否确认删除维修信息表编号为"' + repairIds + '"的数据项？').then(function () {
          return delSysRepairInfo(repairIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/sysRepairInfo/export', {
          ...this.queryParams
        }, `sysRepairInfo_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
